import cv2
import os
import shutil
import tqdm
path=r'/media/alex/FA5EB5A15EB556DB/cellseg'

movepath=r'cellT'
# path=r'E:\zhujinchao\uterus\cellseg\trainmask'




for i in tqdm.tqdm(os.listdir(path)):
    # print(i)
    p1=os.path.join(path,i)
    # print(p1)
    m1=os.path.join(movepath,i)
    if not os.path.exists(m1):
        os.makedirs(m1)
    m1train=os.path.join(m1,'train')
    p1train=os.path.join(p1,'train')
    if not os.path.exists(m1train):
        os.makedirs(m1train)
    m2trainmask=os.path.join(m1,'trainmask')
    p2trainmask=os.path.join(p1,'trainmask')
    if not os.path.exists(m2trainmask):
        os.makedirs(m2trainmask)
    # print(m2trainmask)
    for f in os.listdir(p2trainmask):
        fpath=os.path.join(p2trainmask,f)
        mpath=os.path.join(m2trainmask,f)
        t2path=os.path.join(m1train,f)
        t1path=os.path.join(p1train,f)
        img=cv2.imread(fpath,0)
        ret,thresh=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
        contour,heirarch=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        if len(contour)>0:
            shutil.copy(fpath,mpath)
            # print(fpath,mpath)
            print(t1path,t2path)
            shutil.copy(t1path,t2path)
